package com.hison.tandainbao.controller;

import com.aliyuncs.exceptions.ClientException;
import com.hison.tandainbao.entiy.pojo.domain.User;
import com.hison.tandainbao.entiy.pojo.dto.LoginDto;
import com.hison.tandainbao.entiy.pojo.dto.ResetPassword;
import com.hison.tandainbao.entiy.pojo.dto.SignupDto;
import com.hison.tandainbao.entiy.pojo.dto.UpdateUser;
import com.hison.tandainbao.enums.ResultEnum;
import com.hison.tandainbao.response.ResponseResult;
import com.hison.tandainbao.service.TbUserInfoService;
import com.hison.tandainbao.service.UserService;
import com.hison.tandainbao.util.UserHolder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName: UserController
 * @Descripiton: 用户接口控制类
 * @Author: 小郑
 * @Date： 2024/1/24 22:15
 * @Version：1.8
 **/
@RestController
@RequestMapping("/user")
@Api(value = "用户信息管理接口", tags = "用户信息管理接口")
public class UserController {

    /**
     * 用户基本信息服务类
     */
    @Autowired
    private UserService userService;


    /**
     * 根据id获取用户基本信息
     *
     * @param id
     * @return
     */
//    @GetMapping("/getAll/{id}")
//    @ApiOperation(value = "根据用户id获取用户基本信息", produces = "application/json", httpMethod = "GET")
//    public ResponseResult getAll(@PathVariable("id") @ApiParam("用户id") Long id) {
//        return ResponseResult.ok(ResultEnum.SUCCESS.getDesc(), userService.getById(id));
//    }

    /**
     * 获取用户详细信息
     */
    @GetMapping("/getAllInfo/")
    @ApiOperation(value = "根据用户id获取用户详细信息", produces = "application/json", httpMethod = "GET")
//    public ResponseResult getAllInfo(@PathVariable("id") @ApiParam("用户id") Long id) {？
    public ResponseResult getAllInfo() {
        Long id = UserHolder.getUser().getId();
        return userService.getAllInfo(id);
    }

    /**
     * 用户登录
     *
     * @param login
     * @return
     */
    @PostMapping("/login")
    @ApiOperation(value = "用户登录接口", produces = "application/json", httpMethod = "POST")
    public ResponseResult login(LoginDto login) {
        return userService.login(login);
    }

    /**
     * 用户注册
     *
     * @param signupDto
     * @return
     */
    @PostMapping("/signup")
    @ApiOperation(value = "用户注册接口", produces = "application/json", httpMethod = "POST")
    public ResponseResult signup(@RequestBody SignupDto signupDto) {
        return userService.signup(signupDto);
    }


    /**
     * 根据用户ID编辑用户信息
     *
     * @param updateUser
     * @return
     */
    @PutMapping("/edit")
    @ApiOperation(value = "根据用户ID修改用户信息", produces = "application/json", httpMethod = "PUT")
    public ResponseResult edit(UpdateUser updateUser) {
        //根据用户id，编辑用户信息
        return userService.edit(updateUser);
    }

    /**
     * 重置密码
     *
     * @param resetPassword
     * @return
     * @throws ClientException
     */
    //重置密码，即将密码值为初始值  判断验证码是否正确，正确则重置密码
    @PutMapping("/resetPassword")
    @ApiOperation(value = "根据用户提供的id和电话，重置密码", produces = "application/json", httpMethod = "PUT")
    public ResponseResult resetPassword(@RequestBody ResetPassword resetPassword) throws ClientException {
        return userService.reset(resetPassword);
    }

    /**
     * 获取验证码
     *
     * @param phone
     * @return
     */
    @GetMapping("/getCode")
    @ApiOperation(value = "根据手机号获取验证码", produces = "application/json", httpMethod = "GET")
    public ResponseResult getCode(@Param("Phone") @ApiParam("用户电话") String phone) {
        return userService.senCode(phone);
    }


    /**
     * 登出
     *
     * @param token 令 牌
     * @return @return {@code ResponseResult }
     * @author $USER$
     * @date 2024/03/21
     * @see String
     * @see ResponseResult
     * @since version
     */
    @PostMapping("/logout")
    @ApiOperation(value = "退出登录接口", produces = "application/json", httpMethod = "POST")
    public ResponseResult logOut(@RequestParam("token") String token) {
        UserHolder.removeUser();
        // return userService.logOut(token);
        return ResponseResult.ok("已退出登陆");
    }


    /**
     * 签到
     *
     * @return {@link ResponseResult}
     */
    @PostMapping("/sign")
    @ApiOperation(value = "签到接口", produces = "application/json", httpMethod = "POST")
    public ResponseResult sign() {
        return userService.sign();
    }

    /**
     * 统计签到天数
     *
     * @return {@link ResponseResult}
     */
    @GetMapping("/sign/count")
    @ApiOperation(value = "签到总天数统计接口", produces = "application/json", httpMethod = "POST")
    public ResponseResult signCount() {
        return userService.signCount();
    }

    @PutMapping("/follow")
    @ApiOperation(value = "关注用户", produces = "application/json", httpMethod = "PUT")
    public ResponseResult follow(@ApiParam(value = "userId") @RequestParam("userId") Integer userId) {
        return userService.follow(userId);
    }


    //管理员登录
    @PostMapping("/adminLogin")
    @ApiOperation(value = "管理员登录", produces = "application/json", httpMethod = "POST")
    public ResponseResult getAdminLogin(@ApiParam(value = "phone") @RequestParam("phone") String phone, @ApiParam(value = "password") @RequestParam("password") String password) {
        return  userService.adminLogin(phone, password);
    }



}